package com.demo.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DruidUtil {
    static DataSource ds=null;

    public static DataSource getDataSource(){
        try {
            Properties prop = new Properties();
            InputStream in = DruidUtil.class.getClassLoader().getResourceAsStream("JDBC.properties");
            prop.load(in);

            ds = DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ds;
    }

    public static Connection getConn(){
        try {
            if (ds!=null){
                return ds.getConnection();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
